package com.fanghuaiming.test.netty.c5.future;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import java.util.concurrent.ExecutionException;
import lombok.extern.slf4j.Slf4j;

/****
 * @description:
 * @version:1.0.0
 * @author fanghuaiming
 * @data Created in 2021/9/28 下午11:18
 *
 */
@Slf4j
public class NettyFuturePromise {

  public static void main(String[] args) throws ExecutionException, InterruptedException {
      //1.准备 EventLoop对象
      EventLoop eventLoop = new NioEventLoopGroup().next();
      //2.可以主动创建promise 是一个结果容器
      DefaultPromise<Integer> promise = new DefaultPromise<>(eventLoop);

      new Thread(() ->{
          log.info("执行计算...");
          try {
              int i = 1/0;
              Thread.sleep(1000);
              promise.setSuccess(50);
          } catch (Exception e) {
              e.printStackTrace();
              promise.setFailure(e);
          }
      }).start();

      log.info("等待结果");
      Integer result = promise.get();
      log.info("结果是:{}",result);

  }
}
